package com.ray.controller;

import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ray.exception.ServiceException;
import com.ray.pojo.SSEObject;
import com.ray.util.RequestUtil;
import com.ray.util.redis.RedisCache;
/**
 * "text/event-stream; charset=UTF-8"表明支持SSE这一服务器端推送技术，
     * 字段后面部分"charset=UTF-8"必不可少，否则无法支持SSE。
     *  服务器端发送的数据是连续的Stream，而不是一个数据包，
     * 因此浏览器接收到数据后，不会关闭连接。
 * 
 * @return 服务端向浏览器推送的消息，具有一定的格式要求，详见SSE说明
 */
@Controller
@RequestMapping("sse")
public class SseController {
	/**
	 * 新维修订单
	 * 添加位于/workOrder/addApplyWorkOrder
	 * @param request
	 * @return
	 */
    @RequestMapping(value = "/workOrder/addApplyWorkOrder", produces = "text/event-stream; charset=UTF-8")
    public @ResponseBody String push(HttpServletRequest request) {
    	Map map=RequestUtil.getRequestAllParam(request);
    	Object object = RedisCache.getDataFromRedis(map.get("loginCompany")+"/workOrder/addApplyWorkOrder");
    	RedisCache.delKey(map.get("loginCompany")+"/workOrder/addApplyWorkOrder");
    	if(object!=null) {
    		return "data:" + object + "\n\n"; // SSE 要求的数据格式，详见SSE说明
    	}
        return "data: \n\n"; // SSE 要求的数据格式，详见SSE说明
    }
    
    
    @RequestMapping(value = "/getEquipmentBoardDynamicData", produces = "text/event-stream; charset=UTF-8")
	public @ResponseBody Object getEquipmentBoardDynamicData(
			HttpServletRequest request) throws ServiceException {
		String redisKey="equipmentBoard";
		Map<String, JSONObject> data = (Map<String, JSONObject>)RedisCache.getDataFromRedis(redisKey);
			if (data != null) {
				String result=new SSEObject(JSONObject.toJSON(data)).endMessage();
				return result;
			}
		return new SSEObject().endMessage();
	}
    
    /**
	 * 订单当前操作人 判断订单是否有人在操作
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/isOperateOrder", produces = "text/event-stream; charset=UTF-8")
    public @ResponseBody String isOperateOrder(HttpServletRequest request) {
		Map<String,String> map=RequestUtil.getRequestAllParam(request);
    	Object object = RedisCache.getDataFromRedis("isOperate-"+map.get("loginCompany")+"-"+map.get("orderNo"));
    	if(object!=null) {//有人操作该订单则返回操作人信息
    		JSONObject jsonObject = (JSONObject)JSON.parse(object.toString());
    		if(jsonObject.getString("loginUserName").equals(map.get("loginUserName"))) {//避免是自己操作出现误判
    			RedisCache.updateKeyExpire("isOperate-"+map.get("loginCompany")+"-"+map.get("orderNo"),60);//自己操作依旧延长过期时间 30s
    			return "data: \n\n"; // SSE 要求的数据格式，详见SSE说明
    		}
    		return "data:" + object + "\n\n"; // SSE 要求的数据格式，详见SSE说明
    	}
    	RedisCache.setDataToRedis("isOperate-"+map.get("loginCompany")+"-"+map.get("orderNo"),JSON.toJSON(map),"60");
        return "data: \n\n"; // SSE 要求的数据格式，详见SSE说明
    }
}
